<?php
/**
 * Created by PhpStorm.
 * User: ouryun
 * Date: 2021/5/12
 * Time: 19:11
 */

namespace app\admin\controller;


use think\admin\Controller;
use think\admin\extend\DataExtend;

/**
 * 系统组织
 * Class Department
 * @package app\admin\controller
 */
class Department extends Controller
{
    /**
     * 组织部门表
     * @var string
     */
    protected $table = 'lg_department';

    /**
     * 系统组织列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @auth true
     * @login true
     */
    public function index()
    {
        $this->title = '系统组织';
        $data = $this->request->param();
        $query = $this->_query($this->table)->order('sort DESC');
        if (isset($data['name']) && !empty($data['name'])) {
            $query->whereLike('name', "%{$data['name']}%");
        }
        $query->page();
    }

    /**
     * 处理列表数据
     * @param $list
     */
    protected function _page_filter(&$list)
    {
        $list = DataExtend::arr2table($list);
    }

    /**
     * 添加
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @auth true
     * @login true
     */
    public function add()
    {
        sysoplog('系统组织', '添加数据成功');
        $this->_form($this->table, 'form');
    }

    /**
     * 修改
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @auth true
     * @login true
     */
    public function edit()
    {
        sysoplog('系统组织', '修改数据成功');
        $this->_form($this->table, 'form');
    }

    /**
     * 表单数据处理
     * @param $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(&$data)
    {
        $pList = $this->app->db->table($this->table)->where('pid', 0)->field('id,name')->select();
        $this->assign([
            'pList' => $pList
        ]);
        if ($this->request->isPost()) {
            $params = $this->request->param();
            if (isset($params['id'])) {
                $data['update_time'] = time();
            } else {
                $data['create_time'] = time();
            }
        }
    }

    /**
     * 删除
     * @throws \think\db\exception\DbException
     * @auth true
     * @login true
     */
    public function remove()
    {
        sysoplog('系统组织', '删除数据成功');
        $this->_delete($this->table);
    }
}